package com.eqdd.yiqidian.ui.select;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.util.SparseBooleanArray;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.SelectFriendActivityCustom;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.BaseBean;
import com.eqdd.yiqidian.bean.FriendBean;
import com.eqdd.yiqidian.bean.SelectBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.utils.GsonUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.rong.imlib.model.Conversation;

public class SelectFriendActivity extends BaseActivity {
    private SelectFriendActivityCustom dataBinding;
    private ArrayList<SelectBean> selectBeans;
    private int selectMaxNum;
    private ArrayList<SelectBean> selectedBeans = new ArrayList<>();
    private List<Integer> ints = new ArrayList<>();

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_select_friend);
        initTopTitleBar(View.VISIBLE, "选择好友");
        initTopRightText("提交", v -> {
            getSelectedBeans();
            finishAndResult();
        });
    }

    private void finishAndResult() {
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra(Config.SELECTED_BEANS, selectedBeans);
        setResult(Config.SUCCESS, intent);
        finish();
    }

    private void getSelectedBeans() {
        selectedBeans.clear();
        SparseBooleanArray checkedItemPositions = dataBinding.listView.getCheckedItemPositions();
        for (int i = 0; i < checkedItemPositions.size(); i++) {
            if (checkedItemPositions.valueAt(i)) {
            }
        }
    }

    @Override
    public void initData() {
        selectMaxNum = getIntent().getIntExtra(Config.MAX_NUM, Integer.MAX_VALUE);
        selectBeans = new ArrayList<>();
        new HttpPresneter.Builder(HttpConfig.FRIEND_LIST_INFO, String.class, this)
                .Params("userGuid", user.getUname())
                .setOnHttpListener(new HttpPresneter.OnHttpListener() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Object o) {
                        BaseBean<FriendBean> friendBeanBaseBean = GsonUtils.changeGsonToBaseBean((String) o, FriendBean.class);
                        if (friendBeanBaseBean.getItems() != null) {
                            for (FriendBean friendBean : friendBeanBaseBean.getItems()) {
                                SelectBean selectBean = new SelectBean();
                                selectBean.setGuid(friendBean.getUname());
                                selectBean.setContent(friendBean.getPname());
                                selectBean.setType(Conversation.ConversationType.PRIVATE);
                                selectBean.setHeadImg(friendBean.getIphoto());
                                selectBeans.add(selectBean);
                            }
                            Collections.sort(selectBeans);
//                            if (quickAdapter!=null) {
//                                ArrayList<SelectBean> temp = new ArrayList<>();
//                                temp.addAll(selectBeans);
//                            }
                        }
                    }
                })
                .build()
                .post();
    }
    @Override
    public void setView() {
        dataBinding.rcSidebar.setTextView(dataBinding.rcPopupBg);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }


}
